<?php
/*	
	审核分销商
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();

$tabs=array(
	array('op'=>'list','title'=>'申请列表'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('apply',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'股东','type'=>'member'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'time'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'apply_status'),
			array('name'=>'complete_time','title'=>'审核时间','type'=>'time'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal','op'=>'detail','title'=>'申请信息'),
			array('type'=>'modal','op'=>'check','title'=>'审核','show'=>array('status'=>array(0,2))),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'状态','options'=>'apply_status'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
			
		),
	);
	$return['htmls']=$htmls;
	$return['apply_status']=$this->apply_status;
	exi($return);
}
if($_RQ['op']=='check'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$apply_id=intval($post['id']);
		$status=intval($post['status']);
		if(!$status){
			exi('请选择审核通过还是不通过！','error');
		}
		$this->model('member')->check_agent($apply_id,$status,$post['remark']);
		exi('处理成功！','','/apply/list/');
	}
	$item=$this->get('apply',array('id'=>intval($_RQ['id'])));
	if(!$item){
		exi('指定结算单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'审核分销商申请',
		'edit'=>array(
			array('name'=>'status','title'=>'审核','type'=>'radio','options'=>'apply_status'),
			array('name'=>'remark','title'=>'不通过原因','type'=>'text','show'=>array('status'=>2)),
		),
	);
	$return['apply_status']=$this->apply_status;
	exi($return);
}
if($_RQ['op']=='detail'){
	$id=intval($_RQ['id']);
	$item=$this->get('apply',array('id'=>$id));
	$item=$this->get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
	$item['member']=model('core/member')->detail($item['mid'],array('nickname','avatar','realname','mobile','wechat'));
	$item['member']['avatar']=$item['member']['avatar']?:'../core/resource/images/gw-user.png';
	$item['realname']=$item['realname']?:$item['member']['realname'];
	$item['mobile']=$item['mobile']?:$item['member']['mobile'];
	$item['wechat']=$item['realname']?:$item['member']['wechat'];
	$item['diyformdata']=$item['diyformdata']?json_decode($item['diyformdata'],true):array();
	$formtypes=get_form_types();	
	foreach($item['diyformdata'] as &$v){
		if($v['type']==5 && $v['data']){
			$v['data'] = explode(";", $v['data']);
			foreach($v['data'] as $k=>&$i){
				if(empty($v['data'][$k])){
					unset($v['data'][$k]);
				}
			}
		}
		//$v['type']=$formtypes[$v['type']];
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'look',
		'desc'=>'分销商其他信息',
	);
	$htmls['look']=array(
			array('type'=>'image','title'=>'头像','name'=>'member.avatar'),
			array('type'=>'string','title'=>'昵称','name'=>'member.nickname'),
			array('type'=>'string','title'=>'真实姓名','name'=>'realname'),
			array('type'=>'string','title'=>'电话','name'=>'mobile'),
			array('type'=>'string','title'=>'微信号','name'=>'wechat'),
			array(
						'title'=>'其他信息',
						'type'=>'array',
						'name'=>'diyformdata',
						'list'=>array(
							array('type'=>'string','name'=>'title','title'=>'表单标题'),	
							array('type'=>'labelarray','name'=>'data','title'=>'表单内容'),	
						),
			)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		$this->delete('apply',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->delete('apply',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}